package com.zhenyi.zhengfang.admin.index;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.Kv;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.utils.PathUtils;
import com.youqu.common.model.YqShop;
import com.zhengfang.common.model.ZfComment;
import com.zhengfang.common.model.ZfUser;
import com.zhenyi.zhengfang.config.ZFTemplateUtils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;

public class CommentController extends ZFAdminController {
	@Override
	// 评论列表
	public void list() {
		Date startTime = getStartTime();
		Date endTime = getEndTime();
		Kv kv = getSimpleKv("keyword", "type", "openId");
		kv.set("startTime", startTime);
		kv.set("endTime", endTime);
		SqlPara sqlPara = db.getSqlPara("ZfComment", kv);
		// System.out.println(sqlPara);

		int dl = getParaToInt("dl", 0);
		if (dl == 1) {
			String openId = getPara("openId");
			Kv kvv = Kv.create();
			kvv.set("openId", openId);
			SqlPara sp = db.getSqlPara("ZfComments", kvv);
			List<Record> result = db.find(sp);
			ArrayList<Map<String, Object>> rows = new ArrayList<>();
			for (Record record : result) {
				Map<String, Object> row2 = new LinkedHashMap<>();
				row2.put("姓名", record.get("nickName"));
				row2.put("类型", record.get("type"));
				if ((int) record.get("type") == 0) {
					row2.put("类型", "建议");
				}
				if ((int) record.get("type") == 1) {
					row2.put("类型", "投诉");
				}
				if ((int) record.get("type") == 2) {
					row2.put("类型", "反馈");
				}
				row2.put("提交时间", record.get("addTime"));
				row2.put("内容", record.get("content"));
				row2.put("回复时间", record.get("replyTime"));
				// row2.put("状态", record.get("isReplied"));
				if ((int) record.get("isReplied") == 0) {
					row2.put("状态", "建议");
				}
				if ((int) record.get("isReplied") == 1) {
					row2.put("状态", "投诉");
				}
				row2.put("回复内容", record.get("replyContent"));
				rows.add(row2);

			}
			String path = PathUtils.getFile("data", "zhangfang", DateUtil.today() + "_信箱.xlsx");
			// 通过工具类创建writer
			cn.hutool.poi.excel.ExcelWriter writer = ExcelUtil.getWriter(path);
			// 一次性写出内容
			writer.write(rows);
			// 关闭writer，释放内存
			writer.close();
			renderFile(new File(path));
		} else {
			Page<Record> pages = db.paginate(getPage(), getPageSize(), sqlPara);
			setRainbow(pages);
			setItems(pages.getList());
			render("/zf/admin/comment/list.html");
		}

	}

	@Override
	// 点击回复
	public void info() {
		Long commentId = getParaToLong("commentId");
		String replycontent = getPara("replycontent");
		ZfComment comment = ZfComment.dao.findById(commentId);
		if (comment == null) {
			renderError(404);
			return;
		}
		ZfUser user = ZfUser.dao.findById(comment.getUserId());
		comment.setReplyContent(replycontent);
		comment.setIsReplied(1);
		comment.setReplyTime(new Date());
		boolean flag = comment.update();
		if (flag) {
			ZFTemplateUtils utils = new ZFTemplateUtils();
			utils.sendReplied(user.getOpenId(), comment.getFormId(), commentId);
		}
		renderFlag(flag);
	}

}
